from django.core.urlresolvers import reverse
from django.template.loader import render_to_string
from django.contrib.gis.db import models
import tagging
from tagging.fields import *

class CommonInfo(models.Model):
    title=models.CharField(max_length=600)
    slug=models.SlugField()
    description=models.TextField()
    tags=TagField()
    access_level_required=models.IntegerField(default=0)
    #location=ForeignKey(Location)
    
    def intro(self):
        if len(self.description) > 5000:
            return self[0:5000] + '...'
        else:
            return self.description
    
    def object_name(self):
        return self._meta.object_name

    def __unicode__(self):
        return self.title
    
    def get_absolute_url(self):
        return reverse(self.object_name(), args=(self.slug,))
    
    def html_preview(self):
        return render_to_string('object_preview.html',{'object':self})
        
    def html_full(self):
        return render_to_string('object_full.html',{'object':self})
    
    def html_content(self):
        return self.description()
    
    class Meta:
        abstract = True

class Project(CommonInfo):
    web_link=models.URLField(null=True,blank=True)
    def html_preview(self):
        return render_to_string('project_preview.html',{'project':self})


class ProjectUpdate(CommonInfo):
    date_time=models.DateTimeField(auto_now_add=True)
    project=models.ForeignKey(Project)
    
    current_status=models.CharField(max_length=200)
    class Meta:
        get_latest_by = 'date_time'

class Argument(CommonInfo):
    date_time=models.DateTimeField(auto_now_add=True)
    
    my_answer=models.TextField()
    opposing_answer=models.TextField()
    correct=models.NullBooleanField()

class Place(CommonInfo):
    objects=models.GeoManager()
    coordinates=models.PointField()

class Event(CommonInfo):
    date_time=models.DateTimeField()
    date_time_to=models.DateTimeField(null=True, blank=True)
    place=models.ForeignKey(Place)
    
    def comments(self):
        return render_to_string('comments.html',{'object':self})

class ResumeEntry(CommonInfo):
    ENTRYTYPE_CHOICES=[('academic','Class, conference, etc'),('skill','Learned ability or skill'),('award','Award')]
    entry_type=models.CharField(max_length=20, choices=ENTRYTYPE_CHOICES)
    parent=models.ForeignKey('ResumeEntry', blank=True, null=True)
    SKILL_CHOICES=(
        (1,'Familiar'),
        (2,'Proficient'),
        (3,'Expert'),
        (4,'Guru'),
        )
    skill_level=models.IntegerField(blank=True, null=True, choices=SKILL_CHOICES)
    grade_earned=models.CharField(max_length=20, blank=True, null=True)
    
    def __unicode__(self):
        return self.title
    class Meta:
        verbose_name_plural='Resume Entries'

class Identity(CommonInfo):
    IDTYPE_CHOICES=[('phn','Phone number'),('im','Instant messaging'),('em','Email'),('addr','Address'),('wk','Wiki'),('sn','Social Network')]
    id_type=models.CharField(choices=IDTYPE_CHOICES, max_length=10)
    site=models.CharField(max_length=200, blank=True, null=True)

    class Meta:
        verbose_name_plural='Identities'
    

class Opinion(CommonInfo):
    date_time=models.DateTimeField(auto_now_add=True)
    POSITION_CHOICES=[('1','ardently for'),(2,'for'),(3,'neutral'),(4,'against'),(5,'vehemently against')]
    position=models.IntegerField(choices=POSITION_CHOICES)

class ControlledFile(models.Model):
    name=models.CharField(max_length=2000)
    path=models.CharField(max_length=2000)
    
    def full_path(self):
        return '/home/aaron/'+self.path

    def __unicode__(self):
        return self.name

    def get_absolute_url(self):
        return '/controlled/'+self.name

tagging.register(Event, tag_descriptor_attr='tags_desc')
